package com.example.annotation.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.annotation.common.ResponseUtil;
import com.example.annotation.enums.DataSourceType;
import com.example.annotation.myannotation.DataSourceAnnotation;
import com.example.annotation.service.UserService;


/**
 * @author fz
 * @creation time 2021/5/20
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    //不指定数据源默认查询方法
    @GetMapping("/default")
    public Map<String, Object> getUserDefault() {
        return ResponseUtil.success(userService.getUser());
    }

    //使用自定义注解 数据源是master
    @GetMapping("/master")
    @DataSourceAnnotation(DataSourceType.MASTER)
    public Map<String, Object> getUserFromMaster() {
        return ResponseUtil.success(userService.getUser());
    }

    //使用自定义注解 数据源是slave
    @GetMapping("/slave")
    @DataSourceAnnotation(DataSourceType.SLAVE)
    public Map<String, Object> getUserFromSlave() {
        return ResponseUtil.success(userService.getUser());
    }
}
